"""API 请求和响应数据模型"""
from pydantic import BaseModel, Field
from typing import List, Dict, Optional


class TextInput(BaseModel):
    """单文本输入模型"""
    text: str = Field(..., description="输入文本", min_length=1, max_length=10000)


class TextPairInput(BaseModel):
    """文本对输入模型（用于相似度计算）"""
    text1: str = Field(..., description="第一个文本", min_length=1, max_length=10000)
    text2: str = Field(..., description="第二个文本", min_length=1, max_length=10000)


class Entity(BaseModel):
    """命名实体模型"""
    text: str = Field(..., description="实体文本")
    label: str = Field(..., description="实体类型")
    start: int = Field(..., description="实体在文本中的起始位置")
    end: int = Field(..., description="实体在文本中的结束位置")


class ClassificationResponse(BaseModel):
    """文本分类响应模型"""
    text: str = Field(..., description="输入文本")
    predictions: Dict[str, float] = Field(..., description="各标签的概率分布")
    predicted_label: Optional[str] = Field(None, description="预测的标签")


class SimilarityResponse(BaseModel):
    """文本相似度响应模型"""
    text1: str = Field(..., description="第一个文本")
    text2: str = Field(..., description="第二个文本")
    similarity: float = Field(..., description="相似度分数（0-1）", ge=0.0, le=1.0)


class NERResponse(BaseModel):
    """命名实体识别响应模型"""
    text: str = Field(..., description="输入文本")
    entities: List[Entity] = Field(..., description="识别出的实体列表")
    entity_count: int = Field(..., description="实体数量")


class HealthResponse(BaseModel):
    """健康检查响应模型"""
    status: str = Field(..., description="服务状态")
    message: str = Field(..., description="状态消息")


class ErrorResponse(BaseModel):
    """错误响应模型"""
    error: str = Field(..., description="错误信息")
    detail: Optional[str] = Field(None, description="错误详情")


class SearchRequest(BaseModel):
    """搜索请求模型"""
    years: Optional[List[int]] = Field(None, description="年份列表，如 [2019, 2020, 2021, 2022, 2023, 2024]")
    subjects: Optional[List[str]] = Field(None, description="科目列表，如 ['民法', '刑法']")
    max_results: Optional[int] = Field(100, description="最大搜索结果数")


class SearchResponse(BaseModel):
    """搜索响应模型"""
    search_id: int = Field(..., description="搜索记录ID")
    status: str = Field(..., description="搜索状态")
    message: str = Field(..., description="状态消息")
    total_found: int = Field(0, description="找到的题目总数")
    total_saved: int = Field(0, description="成功保存的题目数")


class QuestionListResponse(BaseModel):
    """题目列表响应模型"""
    total: int = Field(..., description="总题目数")
    questions: List[Dict[str, Any]] = Field(..., description="题目列表")


class QuestionDetailResponse(BaseModel):
    """题目详情响应模型"""
    question_id: str = Field(..., description="题目ID")
    content: str = Field(..., description="题目内容")
    question_type: Optional[str] = Field(None, description="题目类型")
    options: Optional[List[str]] = Field(None, description="选项列表")
    correct_answer: Optional[str] = Field(None, description="正确答案")
    explanation: Optional[str] = Field(None, description="解析")
    subject: Optional[str] = Field(None, description="科目")
    category: Optional[str] = Field(None, description="分类")
    difficulty: Optional[str] = Field(None, description="难度")
    knowledge_points: Optional[List[str]] = Field(None, description="知识点列表")
    year: Optional[int] = Field(None, description="年份")
    source_url: Optional[str] = Field(None, description="来源URL")
    entities: Optional[List[Dict[str, Any]]] = Field(None, description="识别的实体")
    classification: Optional[Dict[str, float]] = Field(None, description="分类结果")

